package com.example.platformhimybodymanage.init;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.example.platformhimybodymanage.cache.RegionCache;
import com.example.platformsharedcommon.Constant;
import com.example.platformsharedcommon.entity.permission.Region;

/**
 * 初始化
 * 
 * @author Dhjie
 *
 */
@Component
@Order(value=1)
public class SimosApplicationRunner implements ApplicationRunner {
	
	private static final Logger logger=LoggerFactory.getLogger(SimosApplicationRunner.class);
	
	@Autowired
	private RedisTemplate<String, Object> redis;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info("==服务启动后，初始化数据操作==");
		redisGet();
	}
	/**
	 * 从reids中获取地区数据 （reids中需要有数据不然会报错）
	 * @param list
	 */
	public void redisGet() {
		logger.info("从redis中获取地区数据");
		Object object = redis.opsForValue().get(Constant.REGION_TO_REDIS);
		String region = JSONObject.toJSONString(object);
		List<Region> regionList = JSONObject.parseArray(region, Region.class);
		RegionCache.RegionToMap(regionList);
		logger.info("地区加载完毕");
	}

}
